<?php

namespace App\Http\Controllers\Site\Custom\Purchaser;

use App\B2BProject\Site\HelperControllerBuildTrait;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\B2BProject\AttachmentPrivateFiles;
use Auth;

class CompanyInformationController extends Controller
{
    use HelperControllerBuildTrait;

    public function index(Request $request)
    {
        $information = Auth::user()->information;

        if ($request->ajax()) {
            return response()->json([
                'data' => [
                    'id'                    => '',
                    'company_email'         => Auth::user()->email,
                    'company'               => $information->company,
                    'legal'                 => $information->legal,
                    'legal_gender'          => $information->legal_gender,
                    'identification_number' => $information->identification_number,
                    'provid'                => $information->province_id,
                    'cityid'                => $information->city_id,
                    'areaid'                => $information->county_id,
                    'address'               => $information->address,
                    'contact_phone'         => $information->contact_phone,
                    'qq'                    => $information->contact_qq,
                    'contact_email'         => $information->contact_email,
                    'email'                 => $information->contact_email,
                    'contact_person'        => $information->contact_person,
                    'lxrsex'                => 0,
                    'elecFiles'             => call_user_func_array(function ($information) {
                        $idA      = $information->legal_identity_a ? route('private.image',
                            [$information->legal_identity_a]) : '';
                        $idB      = $information->legal_identity_b ? route('private.image',
                            [$information->legal_identity_b]) : '';
                        $business = $information->business_license ? route('private.image',
                            [$information->business_license]) : '';

                        $return = [$idA, $idB, $business];

                        return $return;
                    }, [$information]),
                ],
            ]);
        }

        return view('host/custom/purchaser/company-information/index');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Requests\CompanyInformationModifyRequest $request
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Requests\CompanyInformationModifyRequest $request)
    {
        /**
         * @car MemberInformation $information
         */
        $information = Auth::user()->information;

        \DB::transaction(function () use ($request, $information) {
            $data = [
                'legal',
                'legal_gender',
                'legal_identity_a',
                'legal_identity_b',
                'identification_number',
                'address',
                'province_id',
                'city_id',
                'county_id',
                'contact_person',
                'contact_phone',
                'contact_qq',
                'contact_email',
                'business_license',
            ];

            foreach ($data as $name) {
                if (in_array($name, [
                    'legal_identity_a',
                    'legal_identity_b',
                    'business_license',
                ])) {
                    if ($request->hasFile($name) && $request->file($name)->isValid()) {
                        $attach = AttachmentPrivateFiles::findOrFail($information->$name);

                        unlink(base_path(trim($attach->local_path, '/\/')));

                        $attach->delete();

                        $attach             = AttachmentPrivateFiles::setNewUploadFile($request->file($name), "/$name");
                        $information->$name = $attach->id;
                    }
                } else {
                    $information->$name = $request->input($name);
                }
            }

            $information->save();
        });

        return redirect()->route('account.purchaser.company');

    }

    /**
     * 修改密码
     *
     * @param Request $request
     *
     * @return \Response
     */
    public function password(Request $request)
    {
        if (\Hash::check($request->oldpwd, Auth::user()->password)) {
            Auth::user()->password = bcrypt($request->newpwd);
            Auth::user()->save();

            if ($request->ajax()) {
                return response()->json(['data' => ['msg' => 1]]);
            }

            return response('success');
        }

        if ($request->ajax()) {
            return response()->json(['data' => '密码错误'], 403);
        }

        return response('error', 503);
    }

    /**
     * 修改邮件箱
     *
     * @param Request $request
     *
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function email(Request $request)
    {
        if (\Hash::check($request->pwd, Auth::user()->password)) {
            Auth::user()->email = $request->email;
            Auth::user()->save();

            if ($request->ajax()) {
                return response()->json(['data' => ['msg' => 1]]);
            }

            return response('success');
        }

        if ($request->ajax()) {
            return response()->json(['data' => '密码错误'], 403);
        }

        return response('error', 503);
    }
}
